package runesim.node.packethandler;
import org.apache.mina.common.IoSession;
import runesim.node.net.*;
import runesim.node.model.*;
public class CommandHandler implements PacketHandler {
	
	@Override
	public void handlePacket(Packet p, IoSession session) {
		Player player = (Player) session.getAttachment();
		ActionSender sender = player.getActionSender();
		String s = p.readRS2String();
		
		if(player.getPlayerGroup() < 2) return;
		
		String[] tokens = s.split(" "); // command splits
		
		if(tokens[0].equals("getpos")) {
			if(tokens.length > 1) {
				if(tokens[1].equals("this")) {
					sender.sendMessage("Getpos(this): " + player.getLocation());
				} else {
					sender.sendMessage("command format error: unknown getpos target");
				}
			} else {
				sender.sendMessage("command format error: missing second token");
			}
		} else if(tokens[0].equals("goto")){
			if(tokens.length > 2){
				try {
					int x = Integer.parseInt(tokens[1]);
					int y = Integer.parseInt(tokens[2]);
					
					Point current = player.getLocation();
					Point dest = Point.location(x, y);
					
					if((current.getRegionX() == dest.getRegionX()) && (current.getRegionY() == dest.getRegionY())){
						player.setLocation(Point.location(x, y), false);
					} else {
						player.setLocation(Point.location(x, y), true);
					}
					player.setMapAreaChanged(true);
					sender.sendMessage("Goto: x="+x+",y="+y);
				} catch(Exception e){
					sender.sendMessage("command format error: two integers required");
					sender.sendMessage("e.g. goto 3232 3232");
				}
			} else {
				sender.sendMessage("command format error: missing required tokens");
			}
		} else if(tokens[0].equals("testoverhead")){
			player.setTextOverheadMessage("hello from java");
		} else if(tokens[0].equals("testemote")){
			if(tokens.length > 1){
				try {
					int emoteIndex = Integer.parseInt(tokens[1]);
					player.setEmoteIndex(emoteIndex);
					sender.sendMessage("Testemote("+emoteIndex+") playing");
				} catch(Exception e){
					sender.sendMessage("command format error: one integer required");
					sender.sendMessage("e.g. testemote <eID>");
				}
			} else {
				sender.sendMessage("command format error: missing second token");
			}
		} else if(tokens[0].equals("changelevelup")){
			runesim.testing.RuneScriptMethods rsm = new runesim.testing.RuneScriptMethods(player);
			rsm.changelevelup();
		} else if(tokens[0].equals("changeleveldown")){
			runesim.testing.RuneScriptMethods rsm = new runesim.testing.RuneScriptMethods(player);
			rsm.changeleveldown();
		} else if(tokens[0].equals("additem")){
			if(tokens.length > 2){
				try {
					int id = Integer.parseInt(tokens[1]);
					int amount = Integer.parseInt(tokens[2]);
					player.getInventory().addItem(id, amount);
					//sender.sendInventoryItem(id, amount, 0);
					sender.sendMessage("Additem("+id+","+amount+") executed");
				} catch(Exception e){
					sender.sendMessage("command format error: two integers required");
					sender.sendMessage("e.g. additem <id> <amt>");
				}
			} else {
				sender.sendMessage("command format error: missing second token");
			}
		} else if(tokens[0].equals("removeallitems")){
			player.getInventory().emptyInventory();
			sender.sendMessage("RemoveAllItems() executed");
		} else if(tokens[0].equals("shake")){
			if(tokens.length > 1){
				int intensity = Integer.parseInt(tokens[1]);
				sender.sendCameraShake(intensity);
				sender.sendMessage("CameraShake("+intensity+") executed");
			} else {
				sender.sendMessage("command format error: missing second token");
			}
		} else {
			sender.sendMessage("command format error: unknown command tag");
		}
	}
}